package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishSetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFavorMapper dishFavorMapper;
    @Autowired
    DishSetmealMapper dishSetmealMapper;
    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional//为了保证两个表的一致性（新增需要在两个表中都成功才行）
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //先将口味取出
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //获得新增菜品的主键值
        Long id = dish.getId();
        //循环执行将口味附上对应菜品的id
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQueryDish(DishPageQueryDTO dishPageQueryDTO){
         //告知获得第几页，以及每页的数量
         PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
         //将查询结果封装
         Page<DishVO> dishVOS=dishMapper.pageQueryDish(dishPageQueryDTO);
         //将一共可以分为几页，以及返回结果
         return new PageResult(dishVOS.getTotal(),dishVOS.getResult());
    }

    /**
     * 菜品删除  批量删除
     * @param  ids
     * @return
     */
    @Transactional//多表删除的事务一致性
    public void delete(List<Long> ids) {
        //查询菜品是否处于在售状态
        for (Long id : ids) {
             Dish dish=dishMapper.getDishById(id);//返回的状态应该是什么类型。应该由id返回对象查询
             if(dish.getStatus()== StatusConstant.ENABLE){
                 throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
             }
        }
        //查询菜品是否关联了套餐
        log.info("查询是否关联了套餐");
        List<Long> setmealIds=dishSetmealMapper.getSetmealIdBydishId(ids);
        if(setmealIds!=null&&setmealIds.size()>0){//获取集合的大小 setmealIds.size()
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品   ids.for生成快捷的循环语句
        /**
         * 这里重复执行了多条的sql语句，可以使用sql语句的批量删除方式来提高性能
         */

        //for (Long id : ids) {
        //    dishMapper.deleteById(id);//根据菜品id删除菜品
            //将菜品关联的口味删除
        //   dishFavorMapper.deleteByDishId(id);//根据菜品id删除口味
        //  }
        //批量删除
        dishMapper.deleteByIds(ids);
        dishFavorMapper.deleteByDishIds(ids);
    }

    /**
     * 查询菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO = new DishVO();
        //先查询菜品信息
        Dish dish = dishMapper.getDishById(id);
        //查询菜品关联的口味信息   因为再口味表和菜品表都有菜品id
         List<DishFlavor> dishFlavors=dishFavorMapper.getDishFavorByDishId(id);
        //将二者信息封装到dishVO
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        //先将菜品信息进行修改
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        /**
         *  //再将菜品所关联的口味进行删除
         *        // List<DishFlavor> dishFavorByDishId = dishFavorMapper.getDishFavorByDishId(dishDTO.getId());
         *         //重新对菜品关联的口味进行赋值
         */
        //重新插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();//获得dishDto的口味数据
        if (flavors != null && flavors.size() > 0) {//判断口味数据是否为空
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表插入n条数据
            dishFavorMapper.insertBatch(flavors);
        }

    }

}
